package com.example.demo_one

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class CityActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // enableEdgeToEdge()
        setContentView(R.layout.activity_city)

        // 1. 获取到下拉框
        val citySpinner = findViewById<Spinner>(R.id.spinner)
        // 2. 给下拉框设置适配器
        citySpinner.adapter = ArrayAdapter(
            this,
            R.layout.item_select,
            arrayOf("北京", "上海", "广州", "深圳")
        )
        // 3. 设置弹窗的标题
        citySpinner.prompt = "请选择城市"
        // 4. 监听下拉框的点击
        citySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                val city = parent?.getItemAtPosition(position)
                if (city != null) {
                    Toast.makeText(this@CityActivity, city.toString(), Toast.LENGTH_SHORT).show()
                }

            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                TODO("Not yet implemented")
            }
        }
    }
}